<?php

/**
 * Created by PhpStorm.
 * User: mafengli
 * Date: 16/6/14
 * Time: 下午2:42
 */
namespace Web\Controller\Doctor;

/**
 * 前台需继承的控制器类
 * Class BaseController
 * @package Doctor\Controller
 */
class BaseController extends \Think\Controller
{
    /**
     * 视图头部设置
     * @var array
     */
    protected $header = array(
        'h_title'=>'百氏医药福通',
        'h_desc'=>'百氏医药福通',
        'h_keywords'=>'百氏医药福通',
        'h_cur'=>0,
    );

    /**
     * 用户登录id
     * @var null
     */
    protected $userId = null;
    protected $userinfo = null;//用户信息

    protected function _initialize()
    {
        //设置cookie或session参数
        C('COOKIE_PREFIX','ymt_web_doctor');
        C('SESSION_PREFIX','ymt_web_doctor');
        //设置相关信息
        $this->userId = session('user_id');
        $this->handleLogin();
    }

    /**
     * 判断用户是否登录
     * @return bool
     */
    public function isLogin()
    {
        if (!is_null($this->userId)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证用户是否登录,设置用户信息
     * @param $flag bool 默认获取用户信息, 为true时验证登录
     */
    protected function handleLogin($flag=false){
        $userinfo = unserialize(session('userinfo'));
        if($this->userId && !$userinfo){
            //重新设置userinfo
            $userinfo = setUserinfo($this->userId);
            if($userinfo){
                session('userinfo', serialize($userinfo));
            }else{
                if(IS_POST){
                    $this->ajaxReturn(array('code'=>-1,'msg'=>'登录信息异常，请重新登录'));
                }else{
                    cookie("url",$_SERVER['HTTP_REFERER']);
                    $this->redirect('/Web/Doctor/Login/Login');//跳转到登录页
                }
            }
        }
        if(!$userinfo && $flag==true){
            if(IS_POST){
                $this->ajaxReturn(array('code'=>-1,'msg'=>'登录信息异常，请重新登录'));
            }else{
                $this->redirect('/Web/Doctor/Login/Login');//跳转到登录页
            }
        }else{
            //设置UID
            defined('UID') or define('UID',$userinfo['id']);
            $this->userinfo = $userinfo;
        }
    }
}